﻿using System;
using System.Web.UI.WebControls;
using Common;
using Microsoft.Practices.ObjectBuilder;

namespace CSC440TeamOne.MyNutrition.Views
{
    public partial class MyRecipe : Microsoft.Practices.CompositeWeb.Web.UI.Page, IMyRecipeView
    {
        private MyRecipePresenter _presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this._presenter.OnViewInitialized();
            }
            this._presenter.OnViewLoaded();
        }

        [CreateNew]
        public MyRecipePresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }

        private void populate(Recipe recipe)
        {
            this.name.InnerText = recipe.name;
            this.repIngredients.DataSource = recipe.ingredients;
            this.repIngredients.DataBind();
            this.lblDirections.Text = recipe.directions;
            this.PlusMinusButton1.value = recipe.value;
            this.PlusMinusButton1.CommandArgument = recipe.RecipeID.ToString();
            this.hypPrint.NavigateUrl = "PrintRecipe.aspx?RecipeID=" + recipe.RecipeID;
        }

        protected void repIngredients_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ControlBinder<RepeaterItem, RecipeIngredient>.Bind(e.Item, a =>
            {
                a.Find<Label>("Label1").Text = a.BusinessItem.ToString();
            });
        }

        protected void repFavorites_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ControlBinder<RepeaterItem, Recipe>.Bind(e.Item, a =>
            {
                a.Find<LinkButton>("LinkButton1").Text = a.BusinessItem.name;
                a.Find<LinkButton>("LinkButton1").CommandArgument = a.BusinessItem.RecipeID.ToString();
            });
        }

        protected void repSuggested_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ControlBinder<RepeaterItem, Recipe>.Bind(e.Item, a =>
            {
                a.Find<LinkButton>("LinkButton2").Text = a.BusinessItem.name;
                a.Find<LinkButton>("LinkButton2").CommandArgument = a.BusinessItem.RecipeID.ToString();
            });
        }

        protected void repResults_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ControlBinder<RepeaterItem, Recipe>.Bind(e.Item, a =>
            {
                a.Find<LinkButton>("LinkButton1").Text = a.BusinessItem.name;
                a.Find<LinkButton>("LinkButton1").CommandArgument = a.BusinessItem.RecipeID.ToString();
            });
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            MyRecipeViewModel mrvm = _presenter.getRecipesByValue(txtSearch.Text);
            this.repResults.DataSource = mrvm.searchResults;
            this.repResults.DataBind();
        }

        protected void rodName_Click(object sender, EventArgs e)
        {
            MyRecipeViewModel mrvm = _presenter.recipeSelected(Convert.ToInt32(this.rodName.CommandArgument));
            this.populate(mrvm.selectedRecipe);
        }

        protected void repFavorites_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            MyRecipeViewModel mrvm = _presenter.recipeSelected(Convert.ToInt32(e.CommandArgument));
            this.populate(mrvm.selectedRecipe);
        }

        protected void repSuggested_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            MyRecipeViewModel mrvm = _presenter.recipeSelected(Convert.ToInt32(e.CommandArgument));
            this.populate(mrvm.selectedRecipe);
        }

        protected void repResults_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            MyRecipeViewModel mrvm = _presenter.recipeSelected(Convert.ToInt32(e.CommandArgument));
            this.populate(mrvm.selectedRecipe);
        }

        protected void PlusMinusButton1_ClickMinus(object sender, EventArgs e)
        {
            _presenter.OnSave();
        }
        protected void PlusMinusButton1_ClickPlus(object sender, EventArgs e)
        {
            _presenter.OnSave();
        }

        public void Show(MyRecipeViewModel p)
        {
            this.rodName.Text = p.recipeOfTheDay.name;
            this.rodName.CommandArgument = p.recipeOfTheDay.RecipeID.ToString();
            this.populate(p.recipeOfTheDay);

            this.repFavorites.DataSource = p.favoriteRecipes;
            this.repFavorites.DataBind();

            this.repSuggested.DataSource = p.suggestedRecipes;
            this.repSuggested.DataBind();
        }

        public void update(MyRecipeViewModel model)
        {
            model = _presenter.recipeSelected(int.Parse(this.PlusMinusButton1.CommandArgument));
            model.selectedRecipe.value = this.PlusMinusButton1.value;
        }

        public void OnSaveViewCompleted(MyRecipeViewModel model)
        {

        }
    }
}